const phoneStr=/^(13[0-9]|15[0-35-9]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const emailStr=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
const vm=new Vue({
    el:"#area",
    data:{
        area:{},
        province:0,
        provinces:[],
        city:0,
        cities:[],
        county:0,
        counties:[],
        userAddress:{
            id:'',
            userId:'',
            address:'',
            createTime:'',
            isDefault:0,
            userName:'',
            email:'',
            mobile:'',
            area:'',
            remark:'',
            build:'',
            loginName:''
        },
        userUpdateAddress:{
            id:'',
            userId:'',
            address:'',
            createTime:'',
            isDefault:0,
            userName:'',
            email:'',
            mobile:'',
            area:'',
            remark:'',
            build:'',
            loginName:''
        },
        address:[],
        infoFlag:false,
        infoText:'',
        addFlag:false,
        updateFlag:false
    },
    methods:{
        getProvince()
        {
            //加载地区数据
            axios.get("js/area_format_object.json"
            ).then(rs=>{
                this.area=rs.data
                for(let p in this.area)
                {
                    this.provinces.unshift({id:p,name:this.area[p].n})
                }
            })

        },
        getCities()
        {
            this.city=0
            this.county=0
            this.cities=[]
            for(let c in this.area[this.province].c)
            {
                this.cities.unshift({id:c,name:this.area[this.province].c[c].n})
                // console.log(c,this.area[this.province].c[c])
            }
        },
        getCounties()
        {
            this.county=0
            this.counties=[]
            for(let x in this.area[this.province].c[this.city].c)
            {
                this.counties.unshift({id:x,name:this.area[this.province].c[this.city].c[x].n})
                // console.log(this.area[this.province].c[this.city].c[x].n)
            }
        },
        addAddress(opera)
        {
            // console.log(this.provinces)
            // console.log(this.cities)
            // console.log(this.counties)
            if(this.province===0)
            {
                this.infoFlag=true
                this.infoText="请选择省份"
                return
            }
            if(this.city===0)
            {
                this.infoFlag=true
                this.infoText="请选择市"
                return
            }
            if(this.county===0)
            {
                this.infoFlag=true
                this.infoText="请选择区/县"
                return
            }
            //如果是添加操作
            if(opera==="add")
            {
                if(this.userAddress.userName==="")
                {
                    this.infoFlag=true
                    this.infoText="请填写收货人"
                    return
                }
                if(this.userAddress.address==="")
                {
                    this.infoFlag=true
                    this.infoText="请填写详细地址"
                    return
                }
                if(this.userAddress.mobile==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写手机号"
                    return
                }
                if(!phoneStr.test(this.userAddress.mobile))
                {
                    this.infoFlag=true
                    this.infoText="手机号格式不正确（13 | 15 | 17 | 18 | 19开头）"
                    return
                }
                if(this.userAddress.email==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写邮箱"
                    return
                }
                if(!emailStr.test(this.userAddress.email))
                {
                    this.infoFlag=true
                    this.infoText="邮箱格式不正确（xx@ss.ss）"
                    return
                }
                if(this.address.length<5)
                {
                    this.getArea()
                    // console.log(this.userAddress.area)
                    axios.post('/api/address/addUserAddress',
                        this.userAddress
                    ).then(rs=>{
                        if(rs.data.isAdd==="success")
                        {
                            this.infoFlag=true
                            this.infoText="添加成功"
                            this.getAddressList()

                            this.back()
                            setTimeout(()=>{
                                this.infoFlag=false
                            },1000)
                        }else{
                            this.infoFlag=true
                            this.infoText="添加出错了"
                        }

                    })
                }else{
                    this.infoFlag=true
                    this.infoText="地址最多只能有5个，请先整理地址再添加"
                }
            }else{
                if(this.userUpdateAddress.userName==="")
                {
                    this.infoFlag=true
                    this.infoText="请填写收货人"
                    return
                }
                if(this.userUpdateAddress.address==="")
                {
                    this.infoFlag=true
                    this.infoText="请填写详细地址"
                    return
                }
                if(this.userUpdateAddress.mobile==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写手机号"
                    return
                }
                if(!phoneStr.test(this.userUpdateAddress.mobile))
                {
                    this.infoFlag=true
                    this.infoText="手机号格式不正确（13 | 15 | 17 | 18 | 19开头）"
                    return
                }
                if(this.userUpdateAddress.email==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写邮箱"
                    return
                }
                if(!emailStr.test(this.userUpdateAddress.email))
                {
                    this.infoFlag=true
                    this.infoText="邮箱格式不正确（xx@ss.ss）"
                    return
                }
                //修改操作
                this.getArea1()
                if(confirm("确认修改吗"))
                {
                    axios.post('/api/address/updateUserAddress',
                        this.userUpdateAddress).then(rs=>{
                        if(rs.data.isUpdate==="success")
                        {
                            this.infoFlag=true
                            this.infoText="修改成功"
                            this.getAddressList()
                            setTimeout(()=>{
                                this.infoFlag=false
                            },1000)
                        }else{
                            this.infoFlag=true
                            this.infoText="修改出错了"
                        }
                    })
                }

            }

        },
        getAddressList()
        {
            axios.get("/api/address/getAddressList",{
                params:{
                    loginName:this.userAddress.loginName
                }
            }).then(rs=>{
                this.address=rs.data.addressList
            })
        },
        delAddress(id)
        {
            if(confirm("确认删除这个地址吗"))
            {
                axios.get("/api/address/delAddress",{
                    params:{
                        id
                    }
                }).then(rs=>{
                    if(rs.data.isDelete==="yes")
                    {
                        this.infoFlag=true
                        this.infoText="删除成功"
                        this.getAddressList()
                        setTimeout(()=>{
                            this.infoFlag=false
                        },1000)
                    }else{
                        this.infoFlag=true
                        this.infoText="删除出错了"
                    }
                })
            }
          // console.log(id)
        },

        getArea()
        {
            for(let p in this.provinces)
            {
                if(this.provinces[p].id===this.province)
                {
                    this.userAddress.area+=this.provinces[p].name+","
                }
            }
            for(let c in this.cities)
            {
                if(this.cities[c].id===this.city)
                {
                    this.userAddress.area+=this.cities[c].name+","
                }
            }
            for(let p in this.counties)
            {
                if(this.counties[p].id===this.county)
                {
                    this.userAddress.area+=this.counties[p].name
                }
            }
        },
        getArea1()
        {
            this.userUpdateAddress.area=''
            for(let p in this.provinces)
            {
                if(this.provinces[p].id===this.province)
                {
                    this.userUpdateAddress.area+=this.provinces[p].name+","
                }
            }
            for(let c in this.cities)
            {
                if(this.cities[c].id===this.city)
                {
                    this.userUpdateAddress.area+=this.cities[c].name+","
                }
            }
            for(let p in this.counties)
            {
                if(this.counties[p].id===this.county)
                {
                    this.userUpdateAddress.area+=this.counties[p].name
                }
            }
        },
        //设为默认
        changeDefault(id,userId)
        {
            axios.get("/api/address/changeDefault",
                {
                    params:{
                        id,
                        userId
                    }
                }).then(rs=>{
                    if(rs.data.isChange==="yes")
                    {
                        this.infoFlag=true
                        this.infoText="设置成功"
                        setTimeout(()=>{
                            this.infoFlag=false
                        },1000)
                        this.getAddressList()
                    }

            })
        },
        //修改回显
        getUpdateAddress(id)
        {
            for(let a in this.address)
            {
                if(this.address[a].id===id)
                {
                    this.userUpdateAddress=this.address[a]
                    break;
                }
            }
            // console.log(this.userAddress)
            let area=this.userUpdateAddress.area
            areas=area.split(",")
            //省
            let pro=areas[0]
            //市
            let cit=areas[1]
            //区
            let count=areas[2]
            for(let p in this.provinces)
            {
                // console.log(this.provinces[p])
                if(this.provinces[p].name===pro)
                {

                    this.province=this.provinces[p].id
                    this.getCities()
                    break;
                }
            }
            for(let p in this.cities)
            {
                if(this.cities[p].name===cit)
                {
                    this.city=this.cities[p].id
                    this.getCounties()
                    break;
                }
            }
            for(let p in this.counties)
            {
                if(this.counties[p].name===count)
                {
                    this.county=this.counties[p].id
                    break;
                }
            }
            this.updateFlag=true
        },
        back()
        {
            this.addFlag=false
            this.province=0
            this.city=0
            this.county=0
            this.userAddress.area=""
            this.userAddress.email=""
            this.userAddress.mobile=""
            this.userAddress.userName=""
            this.userAddress.build=""
            this.userAddress.remark=""
            this.userAddress.address=""
        },
        closeInfo()
        {
            this.infoFlag=false
        }
    },
    created()
    {
        this.getProvince()
        // console.log(this.provinces)
        this.userAddress.loginName=window.sessionStorage.getItem("loginName")
        this.getAddressList()
    },
})